package com.ericsson.mq.config;

import com.ericsson.mq.constants.RabbitMqConsts;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.CustomExchange;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.ExchangeBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.QueueBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@Configuration
public class RabbitMqConfig {


    /*@Bean
    public RabbitTemplate rabbitTemplate(CachingConnectionFactory connectionFactory) {
        connectionFactory.setPublisherConfirms(true);
        connectionFactory.setPublisherReturns(true);
        RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
        rabbitTemplate.setMandatory(true);
        rabbitTemplate.setConfirmCallback((correlationData, ack, cause) -> log.info("消息发送成功:correlationData({}),ack({}),cause({})", correlationData, ack, cause));
        rabbitTemplate.setReturnCallback((message, replyCode, replyText, exchange, routingKey) -> log.info("消息丢失:exchange({}),route({}),replyCode({}),replyText({}),message:{}", exchange, routingKey, replyCode, replyText, message));
        return rabbitTemplate;
    }*/

    @Bean
    public Queue directQueueAlarm(){
        return QueueBuilder.durable(RabbitMqConsts.QUEUE_ALARM).build();
    }

    @Bean
    public Queue directQueueEops(){
        return QueueBuilder.durable(RabbitMqConsts.QUEUE_EOPS).build();
    }

    @Bean
    public Queue delayQueue(){
        return QueueBuilder.durable(RabbitMqConsts.QUEUE_DELAY).build();
    }

    @Bean
    public DirectExchange directExchange() {
        return ExchangeBuilder.directExchange(RabbitMqConsts.EXCHANGE_NAME).durable(true).build();
    }

    /**
     * 延迟队列交换器
     */
    @Bean
    public CustomExchange delayExchange() {
        Map<String, Object> args = new HashMap<>();
        args.put("x-delayed-type", "direct");
        return new CustomExchange(RabbitMqConsts.EXCHANGE_DELAY_MODE, "x-delayed-message", true, false, args);
    }

    @Bean
    public Binding directBindingAlarm(Queue directQueueAlarm, DirectExchange directExchange) {
        return BindingBuilder.bind(directQueueAlarm).to(directExchange).with(RabbitMqConsts.DIRECT_ROUTING_KEY_ALARM);
    }

    @Bean
    public Binding directBindingEops(Queue directQueueEops, DirectExchange directExchange) {
        return BindingBuilder.bind(directQueueEops).to(directExchange).with(RabbitMqConsts.DIRECT_ROUTING_KEY_EOPS);
    }

    /**
     * 延迟队列绑定自定义交换器
     *
     * @param delayQueue    队列
     * @param delayExchange 延迟交换器
     */
    @Bean
    public Binding delayBinding(Queue delayQueue, CustomExchange delayExchange) {
        return BindingBuilder.bind(delayQueue).to(delayExchange).with(RabbitMqConsts.QUEUE_DELAY).noargs();
    }

}
